//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_SOFTREALTIME_H__
#define __ELASTOS_SOFTREALTIME_H__

#define SOFTREALTIME_NUMBEROF_PRIORITIES    8  // 0 ~ 7
#define SOFTREALTIME_MIN_PRIORITY           0
#define SOFTREALTIME_MAX_PRIORITY \
        (SOFTREALTIME_NUMBEROF_PRIORITIES - 1)

#define SOFTREALTIME_QUOTAS_PER_TICK        16

#define SOFTREALTIME_DEFAULT_PRIORITY       3

enum SoftRealTimeSchedulePolicy
{
    SoftRealTime_RoundRobin,
    SoftRealTime_FIFO
};

//
// SoftRealTimeScheduleClass
//
class SoftRealTimeScheduleClass : public ScheduleClass
{
public:
    virtual void Initialize();

    virtual void InitializeThread(Thread *pThread);

    virtual void SetThreadPriority(
        Thread *pThread, ThreadPriority priority, bool_t bRunning);
    virtual ThreadPriority GetThreadPriority(Thread *pThread);

    virtual void OnThreadSleep(Thread *pThread);
    virtual void OnThreadWakeUp(Thread *pThread);

    virtual void OnThreadYield(Thread *pThread);

    virtual void OnThreadSwitchOut(Thread *pThread);

    virtual void EnterRunningList(Thread *pThread, RunningReason reason);

    virtual void LeaveRunningList(Thread *pThread);

    virtual void UpdateTime(uint_t uTicks);
    virtual bool_t UpdateThreadTime(Thread *pThread, uint_t uTicks);

    virtual Thread *SelectNextThread();

public:
    SoftRealTimeScheduleClass();

private:
    void RecalcMaxActivePriority(int nNewActivePriority);

private:
    uint_t          m_uElapsedTicks;

    DLinkNode       m_listHeads[SOFTREALTIME_NUMBEROF_PRIORITIES];
    int             m_nMaxActivePriority;

    // schedule tables
    STATIC int      s_nBaseTimeQuotas[SOFTREALTIME_NUMBEROF_PRIORITIES];
};

EXTERN SoftRealTimeScheduleClass g_softRealTimeScheduleClass;

#endif // __ELASTOS_SOFTREALTIME_H__
